﻿@using Seal.Model;
@{
    SecurityProvider provider = Model;

    //Parameters for this provider
    provider.Parameters.Add(new SecurityParameter() { Name = "oledb_connection_string", Value = "Provider=SQLOLEDB.1;Persist Security Info=False;Initial Catalog=master;Data Source=(local)", DisplayName = "Database connection string", Description = "The OleDb database connection string used to authenticate the user" });

    provider.Script = @"@using System.Data
@using System.Data.Common
@{
    SecurityUser user = Model;
	//Database authentication script: use the user name and password to authenticate the user and set his name and security groups...
	//user.WebUserName -> user name from the login screen
	//user.WebPassword -> password from the login screen

	user.Name = user.WebUserName; //Display name
	//user.PersonalFolderName -> may be set to defined the personal folder name

	DbConnection connection = Helper.DbConnectionFromConnectionString(ConnectionType.OleDb, Helper.GetOleDbConnectionString(user.Security.GetValue(""oledb_connection_string""), user.WebUserName, user.WebPassword));
	connection.Open();
    //Add default group but the security groups can be got from the database as shown below
    user.AddDefaultSecurityGroup();

    //User default culture, theme and logo can be also overwritten with
    //user.SetDefaultCulture(group.Culture);
    //user.SetDefaultLogoName(group.LogoName);  

    /*
    //OR may come from Northwind: e.g. group=city coming from the employee table
    //Note that we can also use the user name and password to connect to the database
	//User=Fuller,Password=Andrew will return group Tacoma 
	string connectionString = ""Server=localhost;Database=Northwind;Trusted_Connection=True"";
	DbConnection sqlConnection =  Helper.DbConnectionFromConnectionString(ConnectionType.MSSQLServer, connectionString);
	sqlConnection.Open();
	DbCommand command = sqlConnection.CreateCommand();
    command.CommandText = string.Format(""select city from Employees where lastname={0} and firstname={1}"", Helper.QuoteSingle(user.WebUserName), Helper.QuoteSingle(user.WebPassword));
	object groupName = command.ExecuteScalar();
	if (groupName != null && groupName != DBNull.Value)
	{
        user.AddSecurityGroup((string)groupName);	
    }
	else 
	{
		user.Error = ""User unknown from the database."";
	}*/    
}
";    
}
